// package-market/pages/lottery-edit-product/index.js
const app = getApp();
import { PACKAGE_PRODUCT_PAGE } from '../../../constants/route';
import { navTo, navBack, tipToast } from '../../../common/env';
import { arr2Obj, deepClone } from '../../../common/utils';
import { LOTTERY_WIN_RATE, LOTTERY_ACTIVITY_TYPE_RULE } from '../../constants/lottery';
import { getProbability } from '../../biztools/lottery';

Page({
    data: {
        activityInfo: {},
        checkedList: [],
    },
    onLoad(options) {
        app.event.addListener('sendLotteryActivityInfo', this._handleSendActivity.bind(this));
    },
    onUnload() {
        app.event.removeListener('sendLotteryActivityInfo');
    },
    _handleSendActivity(activityInfo) {
        const checkedList = activityInfo.activityProductWithSkus.map(i => i.productId);
        this.setData({
            activityInfo,
            checkedList,
        });
    },
    handleNavToCreate() {
        navTo(PACKAGE_PRODUCT_PAGE['productEdit']);
    },
    handleProductChecked(e) {
        const { ids, products } = e.detail;
        const idsMap = arr2Obj(ids);
        let { activityProductWithSkus = [], lottery1 = {} } = this.data.activityInfo;
        const prizeRatesClone = lottery1.prizeRates ? deepClone(lottery1.prizeRates) : [];
        products.forEach(p => {
            const skus = p.productSpecs && p.productSpecs.length ? p.productSpecs.map(s => {
                s.productId = p.productId;
                s.maximum = '';
                s.price = 0;
                s.skuName = s.name;
                return s;
            }) : [{
                productId: p.productId,
                productSpecId: '0',
                price: 0,
                maximum: '',
            }];
            const ps = {
                productId: p.productId,
                productName: p.name,
                productPics: p.pics,
                priceMin: 0,
                priceMax: 0,
                skus: skus,
                isShow: true,
            };
            if (activityProductWithSkus.findIndex(i => i.productId === p.productId) === -1) {
                activityProductWithSkus.push(ps);
            }
        });
        activityProductWithSkus = activityProductWithSkus.filter(p => idsMap[p.productId]);
        if (lottery1.winRate != LOTTERY_WIN_RATE.custom) {
            lottery1.prizeRates = getProbability(lottery1.winRate, activityProductWithSkus.length);
        } else {
            lottery1.prizeRates = activityProductWithSkus.map((ps, idx) => {
                return prizeRatesClone[idx] || '';
            });
        }
        this.setData({
            checkedList: ids,
            'activityInfo.activityProductWithSkus': activityProductWithSkus,
            'activityInfo.lottery1': lottery1,
        });
    },
    handleSave() {
        const { activityInfo } = this.data;
        const { activityProductWithSkus, lottery1 } = activityInfo;
        const { min, max } = LOTTERY_ACTIVITY_TYPE_RULE[lottery1.uiType];
        if (!activityProductWithSkus || !activityProductWithSkus.length || activityProductWithSkus.length < min || activityProductWithSkus.length > max) {
            tipToast(`请选择${min}-${max}个实物商品`);
            return;
        }
        app.event.trigger('getLotteryActivityInfo', activityInfo);
        navBack();
    },
})
